home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CAS_Content.c
-
- Contains: CASample content element collection implementation
-
- Written by: Rick Badertscher
-
- Copyright © 1993-1995 Apple Computer, All rights reserved.
-
- Change History (most recent first):
-
- 9/1/95 RB Created.
- */
-
- #include "CAS_Globals.h"
- #include "CAS_Content.h"
-
- #ifdef USE_CALIB
- #include "CAS_CAUtil.h"
- #endif
-
- #include "CAS_Doc.h"
- #include "CAS_App.h"
-
- #include "TraceLog.h"
-
- //===========================================================================
- #pragma mark ' Content Element
- //===========================================================================
-
-
- //---------------------------------------------------------------------------
- ElemPtr Elem_Read ( IOStreamPtr ioStream)
- {
- long length;
- long dataLength;
- OSErr theErr;
- ElemPtr theElem;
-
- theElem = (ElemPtr) NewPtr (sizeof (ElemRec));
-
- length = sizeof(ElemRec);
- theErr = CAUtil_ReadWrapper (ioStream, (Ptr) theElem, &length);
-
- theElem->selRgn.cornerHandleRgn = NULL;
- theElem->selRgn.edgeHandleRgn = NULL;
- theElem->selRgn.selectRgn = NULL;
- theElem->collection = NULL;
- theElem->clipRgn = NULL;
- theElem->visible = false;
- theElem->clipInvalid = true;
-
- if (theElem->elemType == kPICTType)
- {
- length = sizeof(long);
- theErr = CAUtil_ReadWrapper (ioStream, (Ptr) &dataLength, &length);
- length = dataLength;
- theElem->elemData = (Handle) NewHandle (dataLength);
- theErr = CAUtil_ReadWrapper (ioStream, (Ptr) *(theElem->elemData), &length);
- }
-
- return theElem;
-
- }
-
-
- //---------------------------------------------------------------------------
- OSErr Elem_Write ( ElemPtr theElem, IOStreamPtr ioStream)
- {
- long length;
- OSErr theErr;
- long dataLength;
-
-
- if (theElem->elemType == kPICTType)
- {
- dataLength = GetHandleSize ((Handle)theElem->elemData);
- }
- else if (theElem->elemType == kFrameElemType)
- {
- dataLength = sizeof (CAFrameRef);
- }
-
- length = sizeof (ElemRec);
- theErr = CAUtil_WriteWrapper (ioStream, (Ptr) theElem, &length);
-
- if (theElem->elemType == kPICTType)
- {
- length = sizeof (long);
- theErr = CAUtil_WriteWrapper (ioStream, (Ptr) &dataLength, &length);
- length = dataLength;
- HLock (theElem->elemData);
- theErr = CAUtil_WriteWrapper (ioStream, (Ptr) *(theElem->elemData), &length);
- HUnlock (theElem->elemData);
-
- }
-
- return theErr;
-
- }
-
- //---------------------------------------------------------------------------
- long Elem_SpaceRequired(ElemPtr theElem)
- {
- long bytes = 0;
-
- bytes = sizeof (ElemRec);
- bytes += sizeof (long);
-
- if (theElem->elemType == kPICTType)
- bytes += GetHandleSize (theElem->elemData);
-
- return (bytes);
- }
-
- //---------------------------------------------------------------------------
- // data is copied
- ElemPtr Elem_New ( Handle data,
- OSType type,
- Rect contentRect,
- Point location)
- {
-
- ElemPtr theElem;
-
- theElem = (ElemPtr) NewPtr (sizeof (ElemRec));
-
- if (type == kFrameElemType)
- theElem->elemData = data;
- else
- {
- // Clone the data handle
- theElem->elemData = (Handle) NewHandle (GetHandleSize (data));
- HLock (theElem->elemData);
- HLock (data);
- BlockMove (*data, *(theElem->elemData), GetHandleSize (data));
- HUnlock (data);
- HUnlock (theElem->elemData);
- }
-
- theElem->elemType = type;
- theElem->location = location;
- theElem->contentRect = contentRect;
- theElem->selected = false;
- theElem->selRgn.cornerHandleRgn = NULL;
- theElem->selRgn.edgeHandleRgn = NULL;
- theElem->selRgn.selectRgn = NULL;
- theElem->collection = NULL;
- theElem->clipRgn = NULL;
- theElem->visible = false;
- theElem->clipInvalid = true;
-
- if (type == kFrameElemType)
- theElem->resizable = true;
- else
- theElem->resizable = false;
-
- return (theElem);
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_UpdateCAFrame (ElemPtr theElem)
- {
- RgnHandle frameRgn;
- Rect contentRect;
-
- if (theElem->elemType != kFrameElemType) return;
-
- // Compute the frame rgn (in frame coordinates)
- Elem_GetRect (theElem, &contentRect);
- frameRgn = NewRgn();
- RectRgn (frameRgn, &contentRect);
-
- // Update the CAFrame
- CASetFrameRgn (ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef) theElem->elemData, frameRgn);
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_UpdateCAVisFrame (ElemPtr theElem)
- {
- CAVisFrame visFrame;
- CATransform extTransform;
- Point location;
- RgnHandle frameRgn;
- Rect contentRect;
-
- if (theElem->elemType != kFrameElemType) return;
-
- visFrame = CAGetVisFrame (ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef) theElem->elemData);
-
- if (!visFrame) return;
-
- // Compute the frame region
- Elem_GetRect (theElem, &contentRect);
- frameRgn = NewRgn();
- RectRgn (frameRgn, &contentRect);
-
- // Compute the external transform
- Elem_GetLocation (theElem, &location);
-
- CAMoveTransformTo (&extTransform, location);
-
- // Update the facet
- CAAdjustVisFrame (visFrame, frameRgn, &extTransform);
-
- }
-
-
-
- //---------------------------------------------------------------------------
- ElemPtr Elem_Clone ( ElemPtr theElem)
- {
-
- ElemPtr newElem;
-
- newElem = (ElemPtr) NewPtr (sizeof (ElemRec));
-
- newElem->elemData = theElem->elemData;
-
- if (theElem->elemType == kFrameElemType)
- newElem->elemData = theElem->elemData;
- else
- {
- // Clone the data handle
- newElem->elemData = (Handle) NewHandle (GetHandleSize (theElem->elemData));
- HLock (newElem->elemData);
- HLock (theElem->elemData);
- BlockMove (*(theElem->elemData), *(newElem->elemData), GetHandleSize (theElem->elemData));
- HUnlock (newElem->elemData);
- HUnlock (theElem->elemData);
- }
-
- newElem->elemType = theElem->elemType;
- newElem->location = theElem->location;
- newElem->contentRect = theElem->contentRect;
- newElem->selected = theElem->selected;
- newElem->selRgn.cornerHandleRgn = NULL;
- newElem->selRgn.edgeHandleRgn = NULL;
- newElem->selRgn.selectRgn = NULL;
- newElem->collection = NULL;
- newElem->clipRgn = NULL;
- newElem->visible = false;
- newElem->clipInvalid = true;
- newElem->resizable = theElem->resizable;
-
- return (newElem);
-
- }
-
-
-
- //---------------------------------------------------------------------------
- void Elem_Remove (ElemPtr theElem)
- {
-
- Rect docRect;
-
- if (theElem->elemType == kFrameElemType)
- {
- CARemoveFrameRef(ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)(theElem->elemData));
- }
-
- Elem_GetLocatedRect (theElem, &docRect);
- InvalRect (&docRect);
-
- }
-
-
- //---------------------------------------------------------------------------
- void Elem_Free (ElemPtr theElem)
- {
-
- if (theElem->resizable)
- {
- if (theElem->selRgn.cornerHandleRgn)
- DisposeRgn (theElem->selRgn.cornerHandleRgn);
-
- if (theElem->selRgn.edgeHandleRgn)
- DisposeRgn (theElem->selRgn.edgeHandleRgn);
-
- if (theElem->selRgn.selectRgn)
- DisposeRgn (theElem->selRgn.selectRgn);
-
- theElem->selRgn.cornerHandleRgn = NULL;
- theElem->selRgn.edgeHandleRgn = NULL;
- theElem->selRgn.selectRgn = NULL;
- }
-
- switch (theElem->elemType)
- {
- case kPICTType:
-
- KillPicture ((PicHandle)theElem->elemData);
- break;
-
- case kFrameElemType:
- break;
-
- default: break;
- }
-
- theElem->elemType = kNULLElemType;
- DisposeRgn (theElem->clipRgn);
-
- DisposePtr ((Ptr)theElem);
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_SetRect( ElemPtr theElem, Rect contentRect)
- {
-
- Rect locatedRect;
-
- Elem_Invalidate (theElem);
-
- if (theElem->elemType == kPICTType)
- {
- theElem->contentRect = contentRect;
-
- }
- else if (theElem->elemType == kFrameElemType)
- {
- Elem_Invalidate(theElem);
- theElem->contentRect = contentRect;
- Elem_CalcSelRgn (theElem);
- }
-
- Elem_GetLocatedRect (theElem, &locatedRect);
-
- ElemColl_ClipElements(theElem->collection, &locatedRect);
- Elem_Invalidate(theElem);
-
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_GetRect( ElemPtr theElem, Rect* contentRect)
- {
-
- *contentRect = theElem->contentRect;
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_GetLocatedRect(ElemPtr theElem, Rect* contentRect)
- {
-
- *contentRect = theElem->contentRect;
- OffsetRect (contentRect, theElem->location.h, theElem->location.v);
-
- }
-
-
- //---------------------------------------------------------------------------
- void Elem_GetDisplayRect(ElemPtr theElem, Rect* displayRect)
- {
-
- switch (theElem->elemType)
- {
-
- case kPICTType:
-
- *displayRect = theElem->contentRect;
- OffsetRect (displayRect, theElem->location.h, theElem->location.v);
-
- break;
-
- case kFrameElemType:
-
- if (theElem->selected)
- {
- *displayRect = (**(theElem->selRgn.selectRgn)).rgnBBox;
- }
- else
- {
- *displayRect = theElem->contentRect;
- OffsetRect (displayRect, theElem->location.h, theElem->location.v);
- }
-
- break;
-
- default: break;
- }
- }
-
-
-
- //---------------------------------------------------------------------------
- void Elem_GetLocation( ElemPtr theElem, Point* location)
- {
- *location = theElem->location;
- }
-
- //---------------------------------------------------------------------------
- void Elem_SetLocation( ElemPtr theElem, Point location)
- {
- Rect affectedArea, initialRect;
-
- // The initial rect
- Elem_GetLocatedRect (theElem, &initialRect);
-
- Elem_Invalidate (theElem);
-
- if (theElem->elemType == kPICTType)
- {
- theElem->location = location;
- }
- else if (theElem->elemType == kFrameElemType)
- {
- theElem->location = location;
- Elem_CalcSelRgn (theElem);
- Elem_UpdateCAVisFrame (theElem);
- }
-
- // The final rect
- Elem_GetLocatedRect (theElem, &affectedArea);
- UnionRect (&initialRect, &affectedArea, &affectedArea);
- ElemColl_ClipElements(theElem->collection, &affectedArea);
-
- Elem_Invalidate(theElem);
-
- }
-
-
- //---------------------------------------------------------------------------
- void Elem_SetSelected( ElemPtr theElem, Boolean selected)
- {
- CAVisFrame visFrame;
-
- if (theElem->elemType == kFrameElemType)
- {
- visFrame = CAGetVisFrame (ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)(theElem->elemData));
- CASetSelected (visFrame, selected);
- }
-
- // Invalidate before we recalculate incase it's deselecting
- Elem_InvalSelRgn (theElem);
-
- theElem->selected = selected;
-
- // Recalc the sel rgns
- Elem_CalcSelRgn (theElem);
-
- // Invalidate
- Elem_Invalidate (theElem);
-
-
- }
-
- //---------------------------------------------------------------------------
- Boolean Elem_GetSelected( ElemPtr theElem)
- {
- return (theElem->selected);
- }
-
- //---------------------------------------------------------------------------
- void Elem_SetVisible( ElemPtr theElem, Boolean visible)
- {
-
- CATransform extTransform;
- CAVisFrame visFrame;
-
- if (visible == false) Elem_Invalidate (theElem);
-
- theElem->visible = visible;
-
- switch (theElem->elemType)
- {
-
- case kPICTType:
- break;
-
- case kFrameElemType:
-
- if (visible)
- {
-
- // Create a facet for the new frame
-
- CAMoveTransformTo (&extTransform, theElem->location);
- CAMakeVisFrame(ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef) theElem->elemData, NULL, &extTransform, NULL);
-
- // Set the facet as selected
- if (theElem->selected)
- {
- visFrame = CAGetVisFrame (ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)(theElem->elemData));
- CASetSelected (visFrame, true);
- }
-
- }
-
- Elem_CalcSelRgn (theElem);
- break;
-
- }
-
- Elem_Invalidate (theElem);
- }
-
-
- //---------------------------------------------------------------------------
- OSType Elem_GetType( ElemPtr theElem)
- {
- return (theElem->elemType);
- }
-
- //---------------------------------------------------------------------------
- void Elem_Draw(ElemPtr theElem, Boolean drawSel)
- {
-
- Rect docRect;
- CAVisFrame visFrame;
-
- if (!theElem->visible) return;
-
- if (theElem->clipInvalid)
- Elem_CalcClipRgn (theElem);
-
- switch (theElem->elemType)
- {
-
- case kPICTType:
-
- Elem_GetLocatedRect (theElem, &docRect);
- DrawPicture( (PicHandle) theElem->elemData, &docRect );
-
- // invert the item (for now) to indicate selection.
- if (drawSel && theElem->selected)
- InvertRect( &docRect );
-
-
- break;
-
- case kFrameElemType:
- {
-
- TRACE (3, "Elem_Draw - FRAME");
-
- // Get the visFrame
- visFrame = CAGetVisFrame( ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)theElem->elemData);
-
- CADrawFrame ( visFrame, NULL);
-
- if (drawSel && theElem->selected)
- Elem_DrawSelRgn (theElem);
- break;
- }
-
- }
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_Invalidate(ElemPtr theElem)
- {
- GrafPtr savePort;
- Rect docRect;
-
- TRACE (3, "Elem_Invalidate");
-
- if (!theElem->visible) return;
-
- GetPort( &savePort );
- SetPort( Doc_GetWindow(ElemColl_GetDocPtr(theElem->collection)));
-
- Elem_GetLocatedRect (theElem, &docRect);
-
- switch (theElem->elemType)
- {
-
- case kPICTType:
- InvalRect (&docRect);
- break;
-
- case kFrameElemType:
- InvalRect (&docRect);
- if (theElem->selected)
- Elem_InvalSelRgn (theElem);
-
- break;
-
- }
-
- SetPort( savePort );
-
- }
-
- void Elem_CalcClipRgn (ElemPtr theElem)
- {
- RgnHandle displayRgn, coveredRgn;
- RgnHandle tempRgn = NULL;
- Rect displayRect;
- CAVisFrame visFrame;
- Boolean anyCovered = false;
-
- if (!theElem->visible) return;
-
- // Reuse clipRgn
- if (theElem->clipRgn == NULL)
- displayRgn = NewRgn();
- else
- displayRgn = theElem->clipRgn;
-
- // Calc the content element display region
- Elem_GetLocatedRect (theElem, &displayRect);
- RectRgn (displayRgn, &displayRect);
-
- // Get the area of the content element which is covered
- coveredRgn = NewRgn();
- ElemColl_GetRgnOverElem (theElem->collection, theElem, coveredRgn);
-
- // Subtract the covered region from the display region
- DiffRgn (displayRgn, coveredRgn, displayRgn);
-
- if (!EmptyRgn (coveredRgn))
- anyCovered = true;
-
- DisposeRgn (coveredRgn);
- theElem->clipRgn = displayRgn;
-
- switch (theElem->elemType)
- {
-
- case kPICTType:
-
- TRACE (3, "Elem_CalcClipRgn for PICT");
- break;
-
- case kFrameElemType:
-
- TRACE (3, "Elem_CalcClipRgn for FRAME");
-
- // Set the clipping region for the embedded frame
- visFrame = CAGetVisFrame( ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)theElem->elemData);
-
- if (anyCovered)
- {
- TRACE (3, "Elem_CalcClipRgn for FRAME, some is covered");
-
- tempRgn = NewRgn();
- CopyRgn (theElem->clipRgn, tempRgn);
- OffsetRgn (tempRgn, - ((**tempRgn).rgnBBox.left), - ((**tempRgn).rgnBBox.top));
- }
- else
- {
- // Clip to the frames used region
- tempRgn = CAGetUsedRgn ( ElemColl_GetDocPtr (theElem->collection)->partDocRef,
- (CAFrameRef)theElem->elemData);
-
- TRACE (3, "Elem_CalcClipRgn for FRAME, NONE is covered");
- }
-
- if (visFrame)
- CAAdjustVisFrame ( visFrame, tempRgn, NULL);
-
- break;
- }
-
- theElem->clipInvalid = false;
-
- }
-
- void Elem_InvalClipRgn (ElemPtr theElem)
- {
- theElem->clipInvalid = true;
- }
-
- //---------------------------------------------------------------------------
- void Elem_DrawSelRgn (ElemPtr theElem)
- {
- WindowPtr theWindow;
- CAHighlight highlight = kCANoHighlight;
- RgnHandle saveClip;
- Rect theRect;
- short baseH, baseV;
-
- if (!theElem->visible) return;
- if (!theElem->selected) return;
-
- theWindow = Doc_GetWindow(ElemColl_GetDocPtr(theElem->collection));
-
- // save the ClipRgn
-
- saveClip = NewRgn();
- GetClip( saveClip );
-
- // Set clip to whole window
-
- theRect = ElemColl_GetDocPtr(theElem->collection)->contentRect;
-
- baseH = GetControlValue( ElemColl_GetDocPtr(theElem->collection)->hScrollBar );
- baseV = GetControlValue( ElemColl_GetDocPtr(theElem->collection)->vScrollBar );
-
- OffsetRect( &theRect, baseH, baseV);
-
- ClipRect (&theRect);
-
- if (Elem_GetSelected (theElem))
- {
- highlight = kCAFullHighlight;
- }
-
- PenMode (patXor);
-
- // If the root frame is not the active frame then draw dimmed selection handles
-
- if ((gFocusDocument != ElemColl_GetDocPtr(theElem->collection)->partDocRef) ||
- gInBackground)
- {
- highlight = kCADimHighlight;
- }
-
- if (highlight == kCAFullHighlight)
- {
-
- FillRgn(theElem->selRgn.selectRgn, &(qd.gray));
- FillRgn(theElem->selRgn.cornerHandleRgn, &(qd.black));
- FillRgn(theElem->selRgn.edgeHandleRgn, &(qd.black));
-
- }
- else if (highlight == kCADimHighlight)
- {
-
- // draw frame border
-
- FillRgn(theElem->selRgn.selectRgn, &(qd.gray));
-
- }
-
- PenNormal();
- SetClip(saveClip);
-
- }
-
- //---------------------------------------------------------------------------
- void Elem_InvalSelRgn (ElemPtr theElem)
- {
-
- if (theElem->selRgn.cornerHandleRgn)
- {
- InvalRgn (theElem->selRgn.cornerHandleRgn);
- InvalRgn (theElem->selRgn.edgeHandleRgn);
- InvalRgn (theElem->selRgn.selectRgn);
- }
-
- }
-
- //---------------------------------------------------------------------------
-
- void Elem_CalcSelRgn (ElemPtr theElem)
- {
- Rect resizeHandle;
- Point handleVertice;
- RgnHandle frameShapeRgn;
- RgnHandle border;
- RgnHandle edgeHdlRgn;
- RgnHandle cornerHdlRgn;
- Rect borderRect;
- RgnHandle coveredRgn;
- Rect rect;
-
- if (theElem->selRgn.cornerHandleRgn)
- DisposeRgn (theElem->selRgn.cornerHandleRgn);
-
- if (theElem->selRgn.edgeHandleRgn)
- DisposeRgn (theElem->selRgn.edgeHandleRgn);
-
- if (theElem->selRgn.selectRgn)
- DisposeRgn (theElem->selRgn.selectRgn);
-
- theElem->selRgn.cornerHandleRgn = NULL;
- theElem->selRgn.edgeHandleRgn = NULL;
- theElem->selRgn.selectRgn = NULL;
-
- if (theElem->selected == false) return;
-
- // -- selection border region --
-
- border = NewRgn();
-
- Elem_GetLocatedRect (theElem, &rect);
- frameShapeRgn = NewRgn();
- RectRgn (frameShapeRgn, &rect);
-
- //frameShapeRgn = CAFrame_GetCAFrameRgn (frame);
- CopyRgn (frameShapeRgn, border);
-
- InsetRgn(border, -1, -1);
- DiffRgn(border, frameShapeRgn, border);
-
- borderRect = (**border).rgnBBox;
-
- // -- corner region --
-
- cornerHdlRgn = NewRgn();
- OpenRgn ();
-
- // Handles
-
- // Left Top
- handleVertice.h = borderRect.left - 2;
- handleVertice.v = borderRect.top - 2;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // Left Bottom
- handleVertice.h = borderRect.left - 2;
- handleVertice.v = borderRect.bottom - 3;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // Right top
- handleVertice.h = borderRect.right - 3;
- handleVertice.v = borderRect.top - 2;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // Right Bottom
- handleVertice.h = borderRect.right - 3;
- handleVertice.v = borderRect.bottom - 3;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- CloseRgn(cornerHdlRgn);
-
- // -- edge handle region --
-
- edgeHdlRgn = NewRgn();
- OpenRgn ();
-
- // Handles
-
- // middle Top
- handleVertice.h = borderRect.left + ((borderRect.right - borderRect.left) / 2) - 2;
- handleVertice.v = borderRect.top - 2;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // middle Bottom
- handleVertice.h = borderRect.left + ((borderRect.right - borderRect.left) / 2) - 2;
- handleVertice.v = borderRect.bottom - 3;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // Left middle
- handleVertice.h = borderRect.left - 2;
- handleVertice.v = borderRect.top + ((borderRect.bottom - borderRect.top) / 2) - 2;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- // Right middle
- handleVertice.h = borderRect.right - 3;
- handleVertice.v = borderRect.top + ((borderRect.bottom - borderRect.top) / 2) - 2;
- SetRect (&resizeHandle, handleVertice.h, handleVertice.v,
- handleVertice.h + kBorderWidth, handleVertice.v + kBorderWidth);
- FrameRect (&resizeHandle);
-
- CloseRgn (edgeHdlRgn);
-
- // coveredRgn represents the display area used
- // by frames above "frame" in the z-order.
-
- coveredRgn = NewRgn();
- ElemColl_GetRgnOverElem ( theElem->collection, theElem, coveredRgn);
-
- // Let frames about "frame" hide the selection regions
-
- DiffRgn (cornerHdlRgn, coveredRgn, cornerHdlRgn);
- DiffRgn (edgeHdlRgn, coveredRgn, edgeHdlRgn);
- DiffRgn (border, coveredRgn, border);
-
- theElem->selRgn.cornerHandleRgn = cornerHdlRgn;
- theElem->selRgn.edgeHandleRgn = edgeHdlRgn;
- theElem->selRgn.selectRgn = border;
-
- Elem_Invalidate (theElem);
-
- }
-
- //---------------------------------------------------------------------------
-
- Boolean Elem_HandleMouseDown ( ElemPtr theElem,
- DocPtr theDoc,
- EventRecord* event)
- {
-
- Boolean retVal = false;
- WindowPtr theWindow;
-
- theWindow = Doc_GetWindow (theDoc);
-
- if (gShiftPressed)
- {
-
- ElemColl_MoveElemToFront(theDoc->contentColl, theElem);
- Elem_SetSelected (theElem, !Elem_GetSelected (theElem));
-
- Win_Update (theWindow);
- Drag_DragItem( theWindow, event);
-
- }
- else
- {
-
- if (theWindow == App_GetFrontDocWindow())
- {
-
- if (!Elem_GetSelected (theElem))
- {
- ElemColl_MoveElemToFront(theDoc->contentColl, theElem);
- Doc_SelectAllElements (theDoc, false);
- Elem_SetSelected (theElem, true);
- }
-
- Win_Update (theWindow);
- Drag_DragItem( theWindow, event);
-
- }
-
- }
-
- return retVal;
-
- }
-
- //---------------------------------------------------------------------------
-
- void Elem_HandleMouseInCornerResize ( ElemPtr theElem,
- DocPtr theDoc,
- Point* mouse,
- EventRecord* event)
- {
- #pragma unused (event)
-
- ResizeHandle growCorner = kNone;
- WindowPtr window;
- Point center;
- Rect sr;
- Rect growBox;
- Rect oldBox;
- Rect qdRect;
- Point delta, theLoc;
- Point lastLoc = {0,0};
- Point transOffset = {0,0};
- GrafPtr curPort;
-
- theLoc = *mouse;
-
- window = Doc_GetWindow (theDoc);
-
- GetPort(&curPort);
- SetPort(window);
-
- Elem_GetLocatedRect(theElem, &sr);
-
- SetPt(¢er, ((sr).right + (sr).left) / 2, ((sr).bottom + (sr).top) / 2); // center of FrameShape
-
- if ((mouse->v <= center.v) && (mouse->h <= center.h)) growCorner = kTL;
- else if ((mouse->v < center.v) && (mouse->h > center.h)) growCorner = kTR;
- else if ((mouse->v > center.v) && (mouse->h < center.h)) growCorner = kBL;
- else if ((mouse->v > center.v) && (mouse->h > center.h)) growCorner = kBR;
-
- growBox = (sr);
- SetRect (&oldBox, 0,0,0,0);
-
- PenMode(patXor);
- PenPat(&(qd.gray));
-
- while (WaitMouseUp())
- {
- GetMouse(&theLoc);
-
- if ((theLoc.h != lastLoc.h) || (theLoc.v != lastLoc.v))
- {
- switch (growCorner)
- {
- case kTL:
- growBox.top = theLoc.v;
- growBox.left = theLoc.h;
- break;
-
- case kTR:
- growBox.top = theLoc.v;
- growBox.right = theLoc.h;
- break;
-
- case kBL:
- growBox.left = theLoc.h;
- growBox.bottom = theLoc.v;
- break;
-
- case kBR:
- growBox.bottom = theLoc.v;
- growBox.right = theLoc.h;
- break;
-
- default:
- break;
- }
-
- FrameRect(&oldBox);
- FrameRect(&growBox);
- }
-
- lastLoc = theLoc;
- oldBox = growBox;
- }
-
- FrameRect(&oldBox);
-
- PenPat(&(qd.gray));
- PenMode(patCopy);
-
- SetOrigin(0,0);
-
- delta.h = theLoc.h - mouse->h; //where.h;
- delta.v = theLoc.v - mouse->v; //where.v;
-
- qdRect = theElem->contentRect;
-
- switch (growCorner)
- {
- case kTL:
-
- if (delta.v > qdRect.bottom)
- delta.v = qdRect.bottom - 1;
- if (delta.h > qdRect.right)
- delta.h = qdRect.right - 1;
-
- qdRect.bottom -= delta.v; qdRect.right -= delta.h;
- transOffset.v += delta.v; transOffset.h += delta.h;
- break;
-
- case kTR:
-
- if (delta.v > qdRect.bottom)
- delta.v = qdRect.bottom - 1;
- if (qdRect.right + delta.h < 0)
- delta.h = - qdRect.right + 1;
-
- qdRect.bottom -= delta.v; qdRect.right += delta.h;
- transOffset.v += delta.v;
- break;
-
- case kBL:
-
- if (qdRect.bottom + delta.v < 0)
- delta.v = - qdRect.bottom + 1;
- if (delta.h > qdRect.right)
- delta.h = qdRect.right - 1;
-
-
- qdRect.bottom += delta.v; qdRect.right -= delta.h;
- transOffset.h += delta.h;
- break;
-
- case kBR:
-
- if (qdRect.bottom + delta.v < 0)
- delta.v = - qdRect.bottom + 1;
- if (qdRect.right + delta.h < 0)
- delta.h = - qdRect.right + 1;
-
- qdRect.bottom += delta.v;
- qdRect.right += delta.h;
- break;
-
- default:
- break;
- }
-
-
- Elem_Invalidate (theElem);
-
- // Resize & relocate the Element
-
- Elem_SetRect (theElem, qdRect);
-
- if ((transOffset.h != 0) || (transOffset.v != 0))
- {
- Point position;
-
- Elem_GetLocation (theElem, &position);
-
- position.v += transOffset.v;
- position.h += transOffset.h;
-
- Elem_SetLocation (theElem, position);
-
- }
-
- ElemColl_MoveElemToFront (theDoc->contentColl, theElem);
-
- // Update the CAFrame & CAVisFrame
- Elem_UpdateCAFrame (theElem);
- Elem_UpdateCAVisFrame (theElem);
-
- SetPort (curPort);
-
- return;
-
- }
-
- //---------------------------------------------------------------------------
-
- void Elem_HandleMouseInEdgeResize ( ElemPtr theElem,
- DocPtr theDoc,
- Point* mouse,
- EventRecord* event)
- {
- #pragma unused (event)
-
- ResizeHandle growEdge = kNone;
- WindowPtr window;
- Point center;
- Rect sr;
- Rect growBox;
- Rect oldBox;
- Rect qdRect;
- Point delta, theLoc;
- Point lastLoc = {0,0};
- Point transOffset = {0,0};
- GrafPtr curPort;
-
- theLoc = *mouse;
-
- window = Doc_GetWindow (theDoc);
-
- GetPort(&curPort);
- SetPort(window);
-
- Elem_GetLocatedRect(theElem, &sr);
-
- SetPt(¢er, ((sr).right + (sr).left) / 2, ((sr).bottom + (sr).top) / 2); // center of FrameShape
-
- if (mouse->v <= (sr.top + (0.1 * sr.top))) growEdge = kTE;
- else if (mouse->h <= (sr.left + (0.1 * sr.left))) growEdge = kLE;
- else if (mouse->v >= (sr.bottom - (0.1 * sr.bottom))) growEdge = kBE;
- else if (mouse->h >= (sr.right - (0.1 * sr.right))) growEdge = kRE;
-
- //this->UTIL_SetGrafPortOrigin(ev, facet);
- growBox = (sr);
- SetRect (&oldBox, 0,0,0,0);
-
- PenMode(patXor);
- PenPat(&(qd.gray));
-
- while (WaitMouseUp())
- {
- GetMouse(&theLoc);
-
- if ((theLoc.h != lastLoc.h) || (theLoc.v != lastLoc.v))
- {
- switch (growEdge)
- {
- case kLE:
- growBox.left = theLoc.h;
- break;
-
- case kRE:
- growBox.right = theLoc.h;
- break;
-
- case kBE:
- growBox.bottom = theLoc.v;
- break;
-
- case kTE:
- growBox.top = theLoc.v;
- break;
-
- default:
- break;
- }
-
- FrameRect(&oldBox);
- FrameRect(&growBox);
- }
-
- lastLoc = theLoc;
- oldBox = growBox;
- }
-
- FrameRect(&oldBox);
-
- PenPat(&(qd.gray));
- PenMode(patCopy);
-
- SetOrigin(0,0);
-
- delta.h = theLoc.h - mouse->h; //where.h;
- delta.v = theLoc.v - mouse->v; //where.v;
-
- qdRect = theElem->contentRect;
-
- switch (growEdge)
- {
- case kLE:
-
- if (delta.h > qdRect.right)
- delta.h = qdRect.right - 1;
- qdRect.right -= delta.h;
- transOffset.h += delta.h;
-
- break;
-
- case kRE:
-
- if (qdRect.right + delta.h < 0)
- delta.h = - qdRect.right + 1;
- qdRect.right += delta.h;
-
- break;
-
- case kTE:
-
- if (delta.v > qdRect.bottom)
- delta.v = qdRect.bottom - 1;
- qdRect.bottom -= delta.v;
- transOffset.v += delta.v;
- break;
-
- case kBE:
-
- if (qdRect.bottom + delta.v < 0)
- delta.v = - qdRect.bottom + 1;
- qdRect.bottom += delta.v;
- break;
-
- default:
- break;
- }
-
-
- Elem_Invalidate (theElem);
-
- // Resize & relocate the Element
-
- Elem_SetRect (theElem, qdRect);
-
- if ((transOffset.h != 0) || (transOffset.v != 0))
- {
- Point position;
-
- Elem_GetLocation (theElem, &position);
-
- position.v += transOffset.v;
- position.h += transOffset.h;
-
- Elem_SetLocation (theElem, position);
-
- }
-
- // Update the CAFrame & CAVisFrame
- Elem_UpdateCAFrame (theElem);
- Elem_UpdateCAVisFrame (theElem);
-
- ElemColl_MoveElemToFront (theDoc->contentColl, theElem);
-
- SetPort (curPort);
-
- return;
-
- }
-
-
- //===========================================================================
- #pragma mark ' Content Element Collection
- //===========================================================================
-
- //---------------------------------------------------------------------------
- void ElemColl_Init( ElemCollPtr theColl, DocPtr theDoc)
- {
-
- theColl->docPtr = theDoc;
- ElemList_Init (&(theColl->elemList));
- }
-
- //---------------------------------------------------------------------------
- // Free the collection, but don't delete the elements
- void ElemColl_Free( ElemCollPtr theColl)
- {
- ElemNodePtr node;
- ElemNodePtr nextNode;
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- while (node)
- {
- // Free the node
- nextNode = node->nextNode;
- DisposePtr ((Ptr) node);
- node = nextNode;
- }
-
- (&(theColl->elemList))->headNode = NULL;
- (&(theColl->elemList))->tailNode = NULL;
- }
-
- //---------------------------------------------------------------------------
- // Free the collection, delete the elements
- void ElemColl_FreeAll( ElemCollPtr theColl)
- {
- ElemNodePtr node;
- ElemNodePtr nextNode;
- ElemPtr elem;
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- while (node)
- {
- // Free the element
- elem = node->elemPtr;
- Elem_Free (elem);
-
- // Free the node
- nextNode = node->nextNode;
- DisposePtr ((Ptr) node);
- node = nextNode;
- }
-
- (&(theColl->elemList))->headNode = NULL;
- (&(theColl->elemList))->tailNode = NULL;
- }
-
-
- //---------------------------------------------------------------------------
- void ElemColl_ClipElements(ElemCollPtr theColl, Rect* clipArea)
- {
- ElemPtr elem;
- ElemNodePtr node;
- Rect rect;
- Rect intersectionRect;
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- while (node)
- {
- elem = node->elemPtr;
- Elem_GetLocatedRect (elem, &rect);
-
- if (clipArea)
- {
-
- // If this element overlaps clipArea then recalc clip region
- SectRect (clipArea, &rect, &intersectionRect);
-
- if (!EmptyRect (&intersectionRect))
- Elem_InvalClipRgn (elem);
-
- }
- else
- Elem_InvalClipRgn (elem);
-
-
- node = node->nextNode;
-
- }
-
- }
-
-
- //---------------------------------------------------------------------------
- void ElemColl_AddElem( ElemCollPtr theColl, ElemPtr theElem)
- {
-
- ElemNodePtr node;
-
- theElem->collection = theColl;
-
- node = (ElemNodePtr) NewPtr (sizeof (ElemNode));
- node->nextNode = NULL;
- node->elemPtr = theElem;
-
- ElemList_AddNode (&(theColl->elemList), node);
-
- }
-
- //---------------------------------------------------------------------------
- void ElemColl_AddElemTemp( ElemCollPtr theColl, ElemPtr theElem)
- {
-
- ElemNodePtr node;
-
- node = (ElemNodePtr) NewPtr (sizeof (ElemNode));
- node->nextNode = NULL;
- node->elemPtr = theElem;
-
- ElemList_AddNode (&(theColl->elemList), node);
-
- }
-
-
-
- //---------------------------------------------------------------------------
- void ElemColl_RemoveElem(ElemCollPtr theColl, ElemPtr theElem)
- {
-
- ElemNodePtr node;
- Rect rect;
-
- node = ElemList_RemoveNode (&(theColl->elemList), theElem);
-
- Elem_GetLocatedRect (theElem, &rect);
- Elem_Invalidate(theElem);
- Elem_Remove (theElem);
- Elem_Free (theElem);
- DisposePtr ((Ptr) node);
-
- ElemColl_ClipElements(theColl, &rect);
-
- }
-
- //---------------------------------------------------------------------------
- short ElemColl_GetCount(ElemCollPtr theColl)
- {
- return (ElemList_GetCount (&(theColl->elemList)));
- }
-
- //---------------------------------------------------------------------------
- short ElemColl_GetCountOfType(ElemCollPtr theColl, OSType elemType)
- {
- short count = 1;
- short total = 0;
- ElemNodePtr node;
-
- if (elemType == kNULLElemType)
- {
- return (ElemList_GetCount (&(theColl->elemList)));
- }
- else
- {
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- while (node)
- {
- if (node->elemPtr->elemType == elemType)
- {
- ++total;
- }
- node = node->nextNode;
- }
- }
-
- return (total);
- }
-
-
- //---------------------------------------------------------------------------
- short ElemColl_FindElem(ElemCollPtr theColl, Point mouse, ElemPtr* theElem)
-
- {
- ElemPtr elem;
- ElemNodePtr node;
- ElemNodePtr matchedNode = NULL;
- short partCode = inElement;
- Rect docRect;
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- while (node)
- {
-
- elem = node->elemPtr;
- Elem_GetLocatedRect (elem, &docRect);
-
- if (PtInRect (mouse, &docRect))
- {
- partCode = inElement;
- matchedNode = node;
- }
- else if (elem->selected)
- {
-
- if (PtInRgn (mouse, elem->selRgn.cornerHandleRgn))
- {
- partCode = inCornerResize;
- matchedNode = node;
- break;
- }
-
- else if (PtInRgn (mouse, elem->selRgn.edgeHandleRgn))
- {
- partCode = inEdgeResize;
- matchedNode = node;
- break;
- }
-
- }
-
- node = node->nextNode;
-
- }
-
- if (matchedNode)
- *theElem = matchedNode->elemPtr;
- else
- *theElem = NULL;
-
- return (partCode);
-
- }
-
-
- //---------------------------------------------------------------------------
- ElemPtr ElemColl_GetNthElem( ElemCollPtr theColl, short index)
- {
- return (ElemList_GetNthElem (&(theColl->elemList), index));
- }
-
- //---------------------------------------------------------------------------
- ElemPtr ElemColl_GetNthElemOfType( ElemCollPtr theColl, short index, OSType type)
- {
-
- ElemPtr elem;
- ElemNodePtr node;
- short count;
- short typeCount = 0;
- Boolean matched = false;
- short listCount;
-
- listCount = ElemList_GetCount (&(theColl->elemList));
-
- if ( (index > listCount) || (index < 1)) DebugStr ("\p ElemColl_GetNthElem - invalid index");
-
- node = ElemList_GetHeadNode (&(theColl->elemList));
-
- for (count = 1; count <= listCount; ++count)
- {
- if (!node) DebugStr ("\pElemColl - list ptr error");
-
- if (node->elemPtr->elemType == type) ++typeCount;
-
- if (typeCount == index)
- {
- matched = true;
- break;
- }
- node = node->nextNode;
-
- }
-
- elem = node->elemPtr;
-
- return (elem);
-
- }
-
- //---------------------------------------------------------------------------
- DocPtr ElemColl_GetDocPtr( ElemCollPtr theColl)
- {
- return (theColl->docPtr);
- }
-
-
- //---------------------------------------------------------------------------
- void ElemColl_MoveElemToFront(ElemCollPtr theColl, ElemPtr theElem)
- {
- ElemNodePtr node;
- Rect rect;
-
- // Move the elemNode to the head of the ElemList
- node = ElemList_RemoveNode (&(theColl->elemList), theElem);
- ElemList_AddNode (&(theColl->elemList), node);
- Elem_GetLocatedRect (theElem, &rect);
- ElemColl_ClipElements(theColl, &rect);
- Elem_Invalidate (theElem);
-
- }
-
-
- //---------------------------------------------------------------------------
- // Computes the region made up of all elements which are above
- // theElem in the z-order.
-
- void ElemColl_GetRgnOverElem (ElemCollPtr theColl, ElemPtr theElem, RgnHandle rgn)
- {
-
- ElemNodePtr node;
- ElemNodePtr tmpNode;
- Rect rect;
- RgnHandle usedRgn;
- Point location;
-
- node = ElemList_GetNode (&(theColl->elemList), theElem);
- tmpNode = ElemList_GetHeadNode (&(theColl->elemList));
-
- // Accumulate the used region of all elements above the target element
- // All nodes after the target node (up to the tail node) are above the target node
- // in the z-order
-
- tmpNode = node;
-
- while (tmpNode->nextNode != NULL)
- {
- tmpNode = tmpNode->nextNode;
-
- if (Elem_GetType (tmpNode->elemPtr) == kFrameElemType)
- {
- usedRgn = CAGetUsedRgn (ElemColl_GetDocPtr (tmpNode->elemPtr->collection)->partDocRef,
- (CAFrameRef)(tmpNode->elemPtr->elemData));
-
- // Transform usedRgn to document coordinates
- Elem_GetLocation (tmpNode->elemPtr, &location);
- OffsetRgn (usedRgn, location.h, location.v);
-
- }
- else
- {
- usedRgn = NewRgn();
- Elem_GetLocatedRect (tmpNode->elemPtr, &rect);
- RectRgn (usedRgn, &rect);
- }
-
- UnionRgn (rgn, usedRgn, rgn);
- DisposeRgn (usedRgn);
-
- // also the selection regions
-
- if (Elem_GetSelected (tmpNode->elemPtr))
- {
- CAHighlight highlight = kCAFullHighlight;
-
- if ((gFocusDocument != ElemColl_GetDocPtr(theElem->collection)->partDocRef) ||
- gInBackground)
- {
- highlight = kCADimHighlight;
- }
-
- if (highlight == kCAFullHighlight)
- {
- UnionRgn (rgn, theElem->selRgn.selectRgn, rgn);
- UnionRgn (rgn, theElem->selRgn.cornerHandleRgn, rgn);
- UnionRgn (rgn, theElem->selRgn.edgeHandleRgn, rgn);
- }
- else if (highlight == kCADimHighlight)
- {
- UnionRgn (rgn, theElem->selRgn.selectRgn, rgn);
- }
-
-
- }
-
- }
-
- }
-
- //---------------------------------------------------------------------------
- ElemListPtr ElemColl_GetCurrentSelection (ElemCollPtr theColl)
- {
-
- ElemListPtr selectionList;
- short count;
- short i;
- ElemNodePtr node;
- ElemPtr elem;
-
- selectionList = (ElemListPtr) NewPtr (sizeof (ElemList));
- ElemList_Init (selectionList);
-
- count = ElemColl_GetCount (theColl);
-
- for (i=1; i <= count; ++i)
- {
- elem = ElemColl_GetNthElem (theColl, i);
-
- if (Elem_GetSelected (elem))
- {
- node = (ElemNodePtr) NewPtr (sizeof (ElemNode));
- node->nextNode = NULL;
- node->elemPtr = elem;
-
- ElemList_AddNode (selectionList, node);
- }
- }
-
- return (selectionList);
-
-
- }
-
-
- //===========================================================================
- #pragma mark ' Content Element List
- //===========================================================================
-
- //---------------------------------------------------------------------------
- void ElemList_Init (ElemListPtr theList)
- {
- theList->headNode = NULL;
- theList->tailNode = NULL;
- theList->count = 0;
- }
-
- //---------------------------------------------------------------------------
- void ElemList_Free (ElemListPtr theList)
- {
-
- ElemNodePtr node;
- ElemNodePtr tmpNode;
-
- node = theList->headNode;
-
- while (node)
- {
-
- tmpNode = node->nextNode;
- DisposePtr ((Ptr) tmpNode);
- node = tmpNode;
-
- }
-
- DisposePtr ((Ptr)theList);
-
- }
-
-
- //---------------------------------------------------------------------------
- void ElemList_AddNode (ElemListPtr theList, ElemNodePtr theNode)
- {
-
- if (theList->headNode)
- {
- theList->tailNode->nextNode = theNode;
- theList->tailNode = theNode;
- }
- else
- {
- theList->headNode = theNode;
- theList->tailNode = theNode;
- }
-
- ++theList->count;
-
- }
-
- //---------------------------------------------------------------------------
- ElemNodePtr ElemList_RemoveNode (ElemListPtr theList, ElemPtr theElem)
- {
-
- ElemNodePtr node;
- ElemNodePtr prevNode;
-
- node = theList->headNode;
- prevNode = NULL;
-
- while (node)
- {
-
- if (node->elemPtr == theElem)
- {
- break;
- }
-
- prevNode = node;
- node = node->nextNode;
-
- }
-
- if (node)
- {
- if (prevNode) prevNode->nextNode = node->nextNode;
- if (node == theList->headNode) theList->headNode = node->nextNode;
- if (node == theList->tailNode) theList->tailNode = prevNode;
- --theList->count;
-
- }
-
- node->nextNode = NULL;
- return (node);
-
- }
-
- //---------------------------------------------------------------------------
- ElemNodePtr ElemList_GetNode (ElemListPtr theList, ElemPtr theElem)
- {
-
- ElemNodePtr tmpNode;
- ElemNodePtr matchedNode;
-
- tmpNode = ElemList_GetHeadNode (theList);
-
- while (tmpNode)
- {
- if (tmpNode->elemPtr == theElem)
- {
- matchedNode = tmpNode;
- break;
- }
- tmpNode = tmpNode->nextNode;
- }
-
- return matchedNode;
-
- }
-
-
- //---------------------------------------------------------------------------
- ElemNodePtr ElemList_GetHeadNode (ElemListPtr theList)
- {
- return (theList->headNode);
- }
-
- //---------------------------------------------------------------------------
- short ElemList_GetCount(ElemListPtr theList)
- {
- return (theList->count);
- }
-
- //---------------------------------------------------------------------------
- ElemPtr ElemList_GetNthElem(ElemListPtr theList, short index)
- {
-
- ElemPtr elem;
- ElemNodePtr node;
- short count;
-
- if ((index > theList->count) || (index < 1)) DebugStr ("\p ElemList_GetNthElem - invalid index");
-
- node = ElemList_GetHeadNode (theList);
-
- for (count = 1; count < index; ++count)
- {
- if (!node) DebugStr ("\pElemList - list ptr error");
-
- node = node->nextNode;
-
- }
-
- elem = node->elemPtr;
-
- return (elem);
-
- }
-
-
-